package com.parking.carsystem.parkingchargesystem.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.parking.carsystem.parkingchargesystem.R;
import com.parking.carsystem.parkingchargesystem.common.Constant;
import com.parking.carsystem.parkingchargesystem.module.GroupListModule;

import java.util.ArrayList;

import static com.parking.carsystem.parkingchargesystem.common.Constant.VIEWTYPE_OTHER;

public class ParkingPlateAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public ArrayList<GroupListModule.DataBean.VehiclesBean> vehicles;

    public Activity activity;

    public ParkingPlateAdapter(ArrayList<GroupListModule.DataBean.VehiclesBean> vehicles, Activity activity) {
        this.vehicles = vehicles;
        this.activity = activity;
    }

    @Override
    public int getItemViewType(int position) {
        GroupListModule.DataBean.VehiclesBean vehiclesBean = vehicles.get(position);
        if (vehiclesBean.itemType.equals("1")) {
            return Constant.VIEWTYPE_THREE;
        }else if(vehiclesBean.itemType.equals("3")){

            return VIEWTYPE_OTHER;
        }else if(vehiclesBean.itemType.equals("2")){
            return Constant.VIEWTYPE_ONDE;
        }
        return Constant.VIEWTYPE_ONDE;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == Constant.VIEWTYPE_OTHER) {
            return new ParkingViewHodler(LayoutInflater.from(activity).inflate(R.layout.item_paking_msg, parent, false));
        } else if(viewType == Constant.VIEWTYPE_THREE) {
            return new AddViewHodler(LayoutInflater.from(activity).inflate(R.layout.item_add_paking, parent, false));
        } else if(viewType == Constant.VIEWTYPE_ONDE){
            return new TitleViewHodler(LayoutInflater.from(activity).inflate(R.layout.item_parking_type, parent, false));
        }
        return new TitleViewHodler(LayoutInflater.from(activity).inflate(R.layout.item_pay_setting_type, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        GroupListModule.DataBean.VehiclesBean vehiclesBean = vehicles.get(position);
        if (vehiclesBean.itemType.equals("3")) {
            ParkingViewHodler parkingViewHodler = (ParkingViewHodler) holder;
            parkingViewHodler.parking_name.setText(vehiclesBean.plateNumber);
            if (vehiclesBean.chooseItem) {
                parkingViewHodler.parking_plate_red.setBackgroundResource(R.drawable.parking_plate_red_stoke);
            } else {
                parkingViewHodler.parking_plate_red.setBackgroundResource(R.color.sc_transparent);
            }
            //正常的车牌
            if (vehiclesBean.plateColor == 1) {
                parkingViewHodler.layout_blue.setText("黄车牌");
                parkingViewHodler.layout_blue.setBackgroundResource(R.drawable.car_parking_yellow_text);
                parkingViewHodler.layout_blue.setTextColor(activity.getResources().getColor(R.color.white));
            } else if (vehiclesBean.plateColor == 0) {
                parkingViewHodler.layout_blue.setText("蓝车牌");
                parkingViewHodler.layout_blue.setBackgroundResource(R.drawable.car_parking_blue_text);
                parkingViewHodler.layout_blue.setTextColor(activity.getResources().getColor(R.color.white));
            } else if (vehiclesBean.plateColor == 2) {
                parkingViewHodler.layout_blue.setText("特种牌");
                parkingViewHodler.layout_blue.setBackgroundResource(R.drawable.car_parking_gray_text);
                parkingViewHodler.layout_blue.setTextColor(activity.getResources().getColor(R.color.white));
            }
        }else if(vehiclesBean.itemType.equals("2")){
            //标题
            TitleViewHodler titleViewHodler = (TitleViewHodler) holder;
            titleViewHodler.company_title.setText(vehiclesBean.plateNumber);
        }
    }

    @Override
    public int getItemCount() {
        return vehicles == null ? 0 : vehicles.size();
    }

    public void setDatas(ArrayList<GroupListModule.DataBean.VehiclesBean> vehicles) {
        this.vehicles = vehicles;
        this.notifyDataSetChanged();
    }

    static class AddViewHodler extends RecyclerView.ViewHolder {

        public AddViewHodler(View itemView) {
            super(itemView);
        }
    }

    static class ParkingViewHodler extends RecyclerView.ViewHolder {
        TextView layout_blue;
        TextView parking_name;
        RelativeLayout parking_plate_red;

        public ParkingViewHodler(View itemView) {
            super(itemView);
            layout_blue = itemView.findViewById(R.id.layout_blue);
            parking_name = itemView.findViewById(R.id.parking_name);
            parking_plate_red = itemView.findViewById(R.id.parking_plate_red);
        }
    }
    static class TitleViewHodler extends RecyclerView.ViewHolder {
        TextView company_title;

        public TitleViewHodler(View itemView) {
            super(itemView);
            company_title = itemView.findViewById(R.id.company_title);
        }
    }
}
